<template>
	<view class="page">
		<u-navbar title="我的红包" border @leftClick="$utils.back()" placeholder />
		<view class="tabBox viewWidth" v-if="!loading">
			<view v-for="item in typeList" :key="item" @click="type=item" :class="{'choose':type==item}">{{item}}</view>
		</view>
		<view class="tabBox viewWidth" v-if="type=='红包记录'&&!loading">
			<view style="border: none;display: flex;align-items: center;" @click="dateShow=true">
				{{date?date:'请选择时间'}}
				<view @click.stop="()=>{}">
					<u-icon @click="clear" name='close-circle' size='28rpx' v-if="date" />
				</view>
			</view>
			<view style="flex: 1;border: none;" />
			<u-icon :label='`：${usersTotal}个`' name="account" size="40rpx"></u-icon>
			<view style="width: 5rpx;border: none;" />
			<u-icon :label='`：￥${totalAmount}`' name="red-packet" size="40rpx"></u-icon>
		</view>
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="type=='红包'">
					<template v-if="!loading">
						<view v-for="item in data" :key="item.id" class="userItem">
							<image @click="jumpUser(item.receiver_id)" class="image" :src="item.receiver.avatar"
								mode="aspectFill"></image>
							<view class="flex">
								<view style="display: flex;align-items: center;">
									<view>
										<u-text :text='item.receiver.nick_name' :lines='1' size='30rpx' />
									</view>
									<view style="margin:0 10rpx;min-width: 235rpx;flex: 1;">
										(未领取:{{item.parts}}个 剩余:￥{{item.total}})
									</view>

									<view style="min-width: 68rpx;">
										{{item.created_at | changeTime}}
									</view>

								</view>
								<view @click.stop="()=>{}" style="display: flex;align-items: center;">
									<view style="flex: 1;">
										<u-text :text='item.blessing_words' :lines='1' size='24rpx' />
									</view>
									<u-icon name="more-dot-fill" size="50rpx" @click='openSelect(item)'></u-icon>
								</view>
							</view>
						</view>
					</template>
					<template v-else>
						<view v-for="item in 10" :key="item" class="userItem">
							<view class="image Skeleton"></view>
							<view class="flex">
								<view style="height: 40rpx;" class="Skeleton"></view>
								<view style="height: 30rpx;" class="Skeleton"></view>
							</view>
						</view>
					</template>
				</template>
				<template v-else-if='type=="红包流水"'>
					<view class="userBox viewWidth" v-if="!loading">
						<view v-for="(item,index) in users" :key="index" @click="receiver_id=item.receiver_id">
							<u-avatar :src='item.receiver.avatar' size='80rpx' />
							<u-text :text='item.receiver.nick_name' :lines='1' size='24rpx' align='center' />
						</view>
					</view>
					<view class="userBox viewWidth" v-else>
						<view v-for="(item,index) in 8" :key="index">
							<view style="width: 80rpx;height: 80rpx;border-radius: 100%;" class="Skeleton" />
							<view style="width: 100%;height: 30rpx;" class="Skeleton" />
						</view>
					</view>
					<template v-if="!loading">
						<view v-for="item in data" :key="item.id" class="userItem">
							<image class="image" :src="item.receiver.avatar" mode="aspectFill"
								@click.stop="jumpRecord(item.receiver_id)"></image>
							<view class="flex">
								<view style="display: flex;justify-content: space-between;">
									<view style="max-width: 60%;">
										<u-text :text='item.receiver.nick_name' :lines='1' size='30rpx' />
									</view>
									<view :style="{color:item.balance>0?'green':'red'}">
										￥：<text v-if="item.balance>0">+</text>{{item.balance}}
									</view>

								</view>
								<view style="display: flex;justify-content: space-between;">
									<view style="max-width: 60%;">
										{{item.created_at | changeTime}}
									</view>
									<view>
										余额：￥{{item.remain }}
									</view>
								</view>
							</view>
						</view>
					</template>
					<template v-else>
						<view v-for="item in 10" :key="item" class="userItem">
							<view class="image Skeleton"></view>
							<view class="flex">
								<view style="height: 40rpx;" class="Skeleton"></view>
								<view style="height: 30rpx;" class="Skeleton"></view>
							</view>
						</view>
					</template>
				</template>
				<template v-else>
					<template v-if="!loading">
						<view v-for="item in data" :key="item.id" class="userItem">
							<image @click="jumpUser(item.receiver_id)" class="image" :src="item.receiver.avatar"
								mode="aspectFill"></image>
							<view class="flex">
								<view style="display: flex;justify-content: space-between;">
									<view style="max-width: 60%;">
										<u-text :text='item.receiver.nick_name' :lines='1' size='30rpx' />
									</view>
								</view>
								<view style="display: flex;justify-content: space-between;">
									<view style="display: flex;">
										<u-icon name='red-packet-fill' color='red'
											:label='`${item.red_packet>0?"+":""}${item.red_packet}`'
											:labelColor="item.red_packet>0?'green':'red'" />
										<view style="width: 10rpx;" />
										<u-text size='24rpx' :lines='1' :text='item.title' color='#ddd' />
									</view>
									<view>
										{{item.created_at | changeTime}}
									</view>
								</view>
							</view>
						</view>
					</template>
					<template v-else>
						<view v-for="item in 10" :key="item" class="userItem">
							<view class="image Skeleton"></view>
							<view class="flex">
								<view style="height: 40rpx;" class="Skeleton"></view>
								<view style="height: 30rpx;" class="Skeleton"></view>
							</view>
						</view>
					</template>
				</template>
			</z-paging>
		</view>
		<view class="viewWidth but" @click="jump" v-if="!loading">
			<view>
				<u-icon name='red-packet-fill' color='#fff' label='发红包' labelColor="#fff" />
			</view>
		</view>
		<root-portal>

			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" showCancelButton @confirm='del()' @cancel='delShow=false'>
				<view style="padding: 12px 25px 25px 25px;font-size: 30rpx;">
					是否确认删除
				</view>
			</u-modal>
			
				<u-datetime-picker title='选择时间' :maxDate='maxDate' @cancel='dateShow=false;' @close='dateShow=false'
					:show="dateShow" v-model="date2" mode="year-month" @confirm='dateConfirm'></u-datetime-picker>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				usersTotal: null,
				totalAmount: null,
				date2: null,
				maxDate: Number(new Date()),
				dateShow: false,
				date: null,
				exclude_ids: [],
				delShow: false,
				selectItem: null,
				selectShow: false,
				receiver_id: null,
				type: '红包',
				typeList: ['红包', '红包流水', '红包记录'],
				data: [],
				loading: true,
				users: []
			}
		},
		computed: {
			columns() {
				return [{
					name: '删除'
				}, {
					name: '编辑'
				}, {
					name: '退出'
				}, ]
			},
			user() {
				return uni.getStorageSync('user')
			}
		},
		watch: {
			receiver_id() {
				console.log(this.users, this.receiver_id)
				if (!this.receiver_id) return
				this.$refs.paging.reload()
			},
			type() {
				this.loading = true
				if (this.type == '红包流水') {
					this.$api.My.Blessing({
						page: 1,
						size: 100
					}).then(res => {
						this.users = res.data.blessings.data
					}).catch(err => {
						uni.$u.toast(err.data.message)
					})
				}
				this.receiver_id = null
				this.$refs.paging.reload()
			}
		},
		onLoad() {},
		methods: {
			clear() {
				this.date = null
				this.date2 = null
				this.$refs.paging.reload()
			},
			dateConfirm(e) {
				var today = new Date(e.value)
			
				var tomorrowYear = today.getFullYear();
				var tomorrowMonth = today.getMonth() + 1; // 月份从0开始，所以要加1
				this.date = tomorrowYear + "-" + tomorrowMonth
				this.dateShow = false
				this.$refs.paging.reload()
			},
			jumpRecord(id) {
				this.$utils.navigateTo({
					url: '/My/blessingRecord?id=' + id
				})
			},
			jumpUser(id) {
				this.$utils.navigateTo({
					url: '/User/run?id=' + id
				})
			},
			del() {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api.Relative.Run.Del(this.selectItem.id).then(err => {
					this.data = this.data.filter(item => item.id != this.selectItem.id)
					
					uni.setStorageSync('user', {
						...this.user,
						account:~~this.user.account+(~~this.selectItem.total)
					});
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
					this.disabled = false
				})
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '删除') {

					this.delShow = true
				} else if (e.name == '退出') {

					this.selectShow = true
				} else if (e.name == '编辑') {
					this.$utils.navigateTo({
						url: '/hongbao/update?id=' + this.selectItem.id
					})
				}


			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = item
			},
			jump() {
				this.$utils.navigateTo({
					url: '/hongbao/add'
				})
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				if (this.receiver_id) {
					obj.receiver_id = this.receiver_id
				}
				uni.showLoading({
					title: '加载中...'
				})
				if (this.type == '红包') {
					this.$api.My.Blessing(obj).then(res => {
						uni.hideLoading()
						this.$refs.paging.complete(res.data.blessings.data)
						this.exclude_ids = res.data.blessings.data.map(item => item.receiver_id)
						uni.setStorageSync('exclude_ids', this.exclude_ids)
						console.log(this.exclude_ids)
						this.loading = false
					}).catch(err => {
						uni.hideLoading()
						uni.$u.toast(err.data.message)
					})
				} else if (this.type == '红包流水') {
					this.$api.My.BlessingBalance(obj).then(res => {
						uni.hideLoading()
						this.$refs.paging.complete(res.data.data)
						this.loading = false
					}).catch(err => {
						uni.hideLoading()
						uni.$u.toast(err.data.message)
					})
				} else {
					if (this.date) {
						obj.date = this.date
					}
					this.$api.My.BlessingRecord(obj).then(res => {
						uni.hideLoading()
						this.usersTotal=res.data.blessings.total
						this.totalAmount=res.data.totalAmount
						this.$refs.paging.complete(res.data.blessings.data)
						this.loading = false
					}).catch(err => {
						uni.hideLoading()
						uni.$u.toast(err.data.message)
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.userItem {
		display: flex;
		margin: 20rpx 0 0 0;
		position: relative;

		.image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 8rpx;
			margin-right: 20rpx;
		}

		.flex {
			height: 100rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding-bottom: 10rpx;
			box-sizing: border-box;
			border-bottom: 1rpx solid #eee;
			flex: 1;

		}
	}

	.but {
		border-radius: 100rpx;
		background-color: red;
		display: flex;
		justify-content: center;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.tabBox {
		display: flex;
		margin: 20rpx 0;

		>view {
			border-radius: 8rpx;
			border: 1rpx solid #ddd;
			padding: 5rpx 10rpx;
			margin-right: 20rpx;
		}
	}

	.choose {
		color: #fff !important;
		background-color: red !important;
		border: none;
	}

	.userBox {
		display: flex;
		align-items: center;
		overflow: auto;

		>view {
			width: 80rpx;
			margin-right: 10rpx;
		}
	}
</style>